/*     */ package com.zimbra.cs.service.formatter;
/*     */ 
/*     */ import com.zimbra.common.localconfig.KnownKey;
/*     */ import com.zimbra.common.localconfig.LC;
/*     */ import com.zimbra.common.service.ServiceException;
/*     */ import com.zimbra.cs.account.Account;
/*     */ import com.zimbra.cs.account.Config;
/*     */ import com.zimbra.cs.account.IDNUtil;
/*     */ import com.zimbra.cs.account.Provisioning;
/*     */ import com.zimbra.cs.fb.FreeBusy;
/*     */ import com.zimbra.cs.fb.FreeBusy.Method;
/*     */ import com.zimbra.cs.mailbox.Appointment;
/*     */ import com.zimbra.cs.mailbox.CalendarItem;
/*     */ import com.zimbra.cs.mailbox.MailItem.Type;
/*     */ import com.zimbra.cs.mailbox.Mailbox;
/*     */ import com.zimbra.cs.service.UserServletContext;
/*     */ import com.zimbra.cs.service.UserServletException;
/*     */ import java.io.IOException;
/*     */ import java.util.EnumSet;
/*     */ import java.util.Map;
/*     */ import java.util.Set;
/*     */ import javax.servlet.ServletOutputStream;
/*     */ import javax.servlet.http.HttpServletRequest;
/*     */ import javax.servlet.http.HttpServletResponse;
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ public class IfbFormatter
/*     */   extends Formatter
/*     */ {
/*     */   private static final long ONE_MONTH = 2678400000L;
/*     */   
/*     */   public FormatterFactory.FormatType getType()
/*     */   {
/*  46 */     return FormatterFactory.FormatType.IFB;
/*     */   }
/*     */   
/*     */   public boolean requiresAuth()
/*     */   {
/*  51 */     return false;
/*     */   }
/*     */   
/*     */   public Set<MailItem.Type> getDefaultSearchTypes()
/*     */   {
/*  56 */     return EnumSet.of(MailItem.Type.APPOINTMENT);
/*     */   }
/*     */   
/*     */   public void formatCallback(UserServletContext context) throws IOException, ServiceException, UserServletException
/*     */   {
/*  61 */     context.resp.setCharacterEncoding("UTF-8");
/*  62 */     context.resp.setContentType("text/calendar");
/*     */     
/*  64 */     long rangeStart = context.getStartTime();
/*  65 */     long rangeEnd = context.getEndTime();
/*     */     
/*  67 */     if (rangeEnd < rangeStart) {
/*  68 */       throw new UserServletException(400, "End time must be after Start time");
/*     */     }
/*  70 */     long days = (rangeEnd - rangeStart) / 86400000L;
/*  71 */     long maxDays = LC.calendar_freebusy_max_days.longValueWithinRange(0L, 36600L);
/*  72 */     if (days > maxDays) {
/*  73 */       throw new UserServletException(400, "Requested range is too large (Maximum " + maxDays + " days)");
/*     */     }
/*  75 */     String acctName = null;
/*  76 */     FreeBusy fb = null;
/*  77 */     if (context.targetMailbox != null) {
/*  78 */       String exuid = (String)context.params.get("exuid");
/*  79 */       Appointment exAppt = null;
/*  80 */       if (exuid != null) {
/*  81 */         CalendarItem ci = context.targetMailbox.getCalendarItemByUid(context.opContext, exuid);
/*  82 */         if ((ci instanceof Appointment))
/*  83 */           exAppt = (Appointment)ci;
/*     */       }
/*  85 */       acctName = context.targetMailbox.getAccount().getName();
/*  86 */       fb = context.targetMailbox.getFreeBusy(context.opContext, acctName, rangeStart, rangeEnd, context.getFreeBusyCalendar(), exAppt);
/*     */     }
/*     */     else {
/*  89 */       acctName = fixupAccountName(context.accountPath);
/*  90 */       fb = FreeBusy.emptyFreeBusy(acctName, rangeStart, rangeEnd);
/*     */     }
/*     */     
/*  93 */     String fbFormat = (String)context.params.get("fbfmt");
/*     */     String fbMsg;
/*  95 */     String fbMsg; if ("event".equalsIgnoreCase(fbFormat)) {
/*  96 */       fbMsg = fb.toVCalendarAsVEvents();
/*     */     } else {
/*  98 */       String url = context.req.getRequestURL() + "?" + context.req.getQueryString();
/*  99 */       fbMsg = fb.toVCalendar(FreeBusy.Method.PUBLISH, acctName, null, url);
/*     */     }
/* 101 */     context.resp.getOutputStream().write(fbMsg.getBytes("UTF-8"));
/*     */   }
/*     */   
/*     */   public long getDefaultStartTime()
/*     */   {
/* 106 */     return System.currentTimeMillis() - 2678400000L;
/*     */   }
/*     */   
/*     */ 
/*     */   public long getDefaultEndTime()
/*     */   {
/* 112 */     return System.currentTimeMillis() + 5356800000L;
/*     */   }
/*     */   
/*     */   private String fixupAccountName(String emailAddress)
/*     */     throws ServiceException
/*     */   {
/* 118 */     int index = emailAddress.indexOf('@');
/* 119 */     String domain = null;
/* 120 */     if (index == -1)
/*     */     {
/* 122 */       domain = Provisioning.getInstance().getConfig().getAttr("zimbraDefaultDomainName", null);
/* 123 */       if (domain != null)
/* 124 */         emailAddress = emailAddress + "@" + domain;
/*     */     } else {
/* 126 */       emailAddress = IDNUtil.toAsciiEmail(emailAddress);
/*     */     }
/* 128 */     return emailAddress;
/*     */   }
/*     */ }


/* Location:              /home/mint/zimbrastore.jar!/com/zimbra/cs/service/formatter/IfbFormatter.class
 * Java compiler version: 7 (51.0)
 * JD-Core Version:       0.7.1
 */